package com.banzhi.shop.view;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.banzhi.shop.R;


/**
 * 自定义 ProgressDialog 实现 loadingDialog
 */
public class CustomProgressDialog extends Dialog {

    private static CustomProgressDialog customProgressDialog = null;

    public CustomProgressDialog(Context context, int theme) {
        super(context, theme);
    }

    public static CustomProgressDialog createDialog(Context context) {

        customProgressDialog = new CustomProgressDialog(context, R.style.CustomProgressDialog);
        customProgressDialog.setContentView(R.layout.loading_dialog);
        customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
//        customProgressDialog.setCancelable(false);//点击返回键不消失
        customProgressDialog.setCanceledOnTouchOutside(false);//点击对话框外部消失
        return customProgressDialog;
    }

    //可交换时
    public void onWindowFocusChanged(boolean hasFocus) {

        if (customProgressDialog == null) {
            return;
        }
        ImageView img = (ImageView) customProgressDialog.findViewById(R.id.iv_loading);
        RotateAnimation rotateAnimation = (RotateAnimation) AnimationUtils.loadAnimation(getContext(), R.anim.loading_animation);
        img.startAnimation(rotateAnimation);
    }


    //标题
    public CustomProgressDialog setTitile(String strTitle) {
        return customProgressDialog;
    }

    //提示内容
    public CustomProgressDialog setMessage(String strMessage) {
        TextView tvMsg = (TextView) customProgressDialog.findViewById(R.id.tv_loading);

        if (tvMsg != null) {
            tvMsg.setText(strMessage);
        }

        return customProgressDialog;
    }
}